from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SPU, Brand, GoodsCategory
from apps.meiduo_admin.serializers.spu import SPUGoodsSerializer, BrandSerailizer, GoodsCategorySerailizer
from apps.meiduo_admin.utils import PageNum


class SPUGoodsView(ModelViewSet):
    """
    SPU表的获取
    """
    # 指定序列化器
    serializer_class = SPUGoodsSerializer
    # 指定查询集
    queryset = SPU.objects.all()
    # 指定分页器
    pagination_class = PageNum

    # 获取品牌信息
    def brands(self,request):
        # 获取所有品牌
        data = Brand.objects.all()
        # 返回品牌数据
        ser = BrandSerailizer(data,many=True)
        return Response(ser.data)

    # 获取一级分类
    def channel(self,request):
        # 查询一级分类
        data = GoodsCategory.objects.filter(parent=None)
        # 返回分类信息
        ser = GoodsCategorySerailizer(data,many=True)
        return Response(ser.data)

    # 获取二级分类
    def channel2(self,request,pk):
        # 查询二级分类
        data = GoodsCategory.objects.filter(parent=pk)
        # 返回分类信息
        ser = GoodsCategorySerailizer(data,many=True)
        return Response(ser.data)
